package com.thinkgem.jeesite.frame.pm.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.frame.pm.entity.PmDataTree;
import com.thinkgem.jeesite.frame.pm.service.PmDataTreeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 数据字典Controller
 *
 * @author kongxiangsheng
 * @version 2018-09-02
 */
@Controller
@RequestMapping(value = "${adminPath}/pm/pmDataTree")
public class PmDataTreeController extends BaseController {
    @Autowired
    private PmDataTreeService pmDataTreeService;

    @RequiresPermissions("pm:pmDataTree:view")
    @RequestMapping(value = {""})
    public String index(PmDataTree pmDataTree, Model model) {
        return "frame/pm/PmDataTree/index";
    }

    @RequiresPermissions("pm:pmDataTree:view")
    @RequestMapping(value = {"list"})
    public String list(PmDataTree pmDataTree, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<PmDataTree> page = new BasePage<PmDataTree>(request, response);
        pmDataTreeService.page(page, new QueryWrapper<PmDataTree>(pmDataTree));

        model.addAttribute("page", page);
        return "frame/pm/PmDataTree/list";
    }

    @RequiresPermissions("pm:pmDataTree:view")
    @RequestMapping(value = "form")
    public String form(PmDataTree pmDataTree, Model model) {
        if (StringUtils.isNotBlank(pmDataTree.getId())) {
            pmDataTree = pmDataTreeService.getById(pmDataTree.getId());
        }
        model.addAttribute("pmDataTree", pmDataTree);
        return "frame/pm/PmDataTree/form";
    }

    @RequiresPermissions("pm:pmDataTree:edit")
    @RequestMapping(value = "save")
    public String save(PmDataTree pmDataTree, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, pmDataTree)) {
            return form(pmDataTree, model);
        }
        pmDataTreeService.updateById(pmDataTree);
        addMessage(redirectAttributes, "保存数据字典成功");
        return "redirect:" + adminPath + "/pm/pmDataTree/?repage";
    }

    @RequiresPermissions("pm:pmDataTree:edit")
    @RequestMapping(value = "delete")
    public String delete(PmDataTree pmDataTree, RedirectAttributes redirectAttributes) {
        pmDataTreeService.removeById(pmDataTree.getId());
        addMessage(redirectAttributes, "删除数据字典成功");
        return "redirect:" + adminPath + "/pm/pmDataTree/?repage";
    }

    /**
     * 获取树形参数JSON数据。
     *
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();

        // 添加父节点
        Map<String, Object> parentMap = Maps.newHashMap();
        parentMap.put("id", "1");
        parentMap.put("pId", "0");
        parentMap.put("pIds", "0,");
        parentMap.put("name", "所有树形字典");
        parentMap.put("isParent", true);
        mapList.add(parentMap);

        // 添加所有子节点
        QueryWrapper queryWrapper = new QueryWrapper<PmDataTree>();
        queryWrapper.select(PmDataTree.COL_ID, PmDataTree.COL_TREE_CODE, PmDataTree.COL_TREE_NAME);
        List<PmDataTree> list = pmDataTreeService.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            PmDataTree pmDataTree = list.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", pmDataTree.getId());
            map.put("pId", "1");
            map.put("pIds", "0,1,");
            map.put("name", pmDataTree.getTreeName() + "(" + pmDataTree.getTreeCode() + ")");
            map.put("isParent", false);
            mapList.add(map);
        }
        return mapList;
    }

}